from Engine import *
from Tkinter import *

class Editor:
    def __init__(self, guiMaster):
        self.guiMaster = guiMaster
        self.gui = Frame(guiMaster)
        self.gui.pack()
        
        self.btnBegin = Button(self.gui, width=6, text="Begin", command=self.begin)
        self.btnBegin.pack(side=LEFT, padx=10, pady=10)
        
        # Create game engine instance
        self.engine = Engine()
        
        # Engine settings
        self.engine.tickRate = 60
        self.engine.gravity = 0.0
        self.engine.worldMinX = -10.0
        self.engine.worldMinY = -10.0
        self.engine.worldMaxX = 10.0
        self.engine.worldMaxY = 10.0
        # TODO: Work out why turning this on makes the physics go haywire
        self.engine.usePhysicsSleeping = False
        self.engine.windowWidth = 500
        self.engine.windowHeight = 600
        self.engine.forceResolution = True
        self.engine.fullscreen = False
        self.engine.physicsIterations = 10
        self.engine.init()
        
        #engine.addEventListener('input', self.root.handleOSEvent)
        self.engine.addEventListener('tick', self.onTick)
        
        self.engine.renderer.camera.setMaxZoomRate(0.01)
        self.engine.renderer.camera.setZoomAccel(0.02)
        self.engine.renderer.camera.zoomTo(1.0)
        self.engine.renderer.camera.moveDirectTo( (0.0, 1.0) )

        groundShapeDef = b2PolygonDef()
        groundShapeDef.SetAsBox(10.0, 1.0, b2Vec2(0.0, -1.0), 0.0)
        self.engine.world.GetGroundBody().CreateShape(groundShapeDef)
        
        self.engine.entities["CharacterBody"] = CharacterBody(self.engine, b2Vec2(0.0, 1.0))
        
        pinJointDef = b2RevoluteJointDef()
        pinJointDef.Initialize(self.engine.entities["CharacterBody"].bodies["LowerTorso"], \
                            self.engine.world.GetGroundBody(), \
                            self.engine.entities["CharacterBody"].bodies["LowerTorso"].GetWorldCenter())
        pinJointDef.lowerAngle = 0.0
        pinJointDef.upperAngle = 0.0
        pinJointDef.enableLimit = True
        self.engine.world.CreateJoint(pinJointDef)
        
        self.engine.run()
        
    def begin(self):
        print "Begin"
        
    def onTick(self, engine, timeStep):
        self.guiMaster.update()
        character = self.engine.entities["CharacterBody"]
        character.applyDampingForce(0.5)
        character.applyDampingTorque(0.005)
        
root = Tk()
#root.wm_state('zoomed')
app = Editor(root)
#root.mainloop()